package cn.edu.hpu.action;


import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.hpu.pojo.Contacts;
import cn.edu.hpu.pojo.Deal;
import cn.edu.hpu.pojo.Task;

import cn.edu.hpu.pojo.Dealnote;

import cn.edu.hpu.pojo.Dealtype;
import cn.edu.hpu.pojo.Message;

import cn.edu.hpu.pojo.User;
import cn.edu.hpu.pojo.Usergroup;
import cn.edu.hpu.service.ContactsService;
import cn.edu.hpu.service.DealService;
import cn.edu.hpu.service.TaskService;

import cn.edu.hpu.service.DealnoteService;

import cn.edu.hpu.service.DealtypeService;
import cn.edu.hpu.service.MessageService;

import cn.edu.hpu.service.UserService;
import cn.edu.hpu.service.UsergroupService;
import cn.edu.hpu.util.DateUtil;
import cn.edu.hpu.util.TimeUtil;
import cn.edu.hpu.util.UUIDUtil;


import com.opensymphony.xwork2.ActionSupport;

public class DealAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private DealService dealService;
	private List<Deal> dealList1;
	private List<Deal> dealList2;
	private List<Deal> dealList3;
	private String id;
	private Deal deal;
	private ContactsService contactsService;
	private List<Contacts> contactsList;
	private UserService userService;
	private MessageService messageService;
	private UsergroupService usergroupService;
	private String uid;

	private Contacts contacts;
	private String did;
	private List<Dealnote> dealnoteList;
	private DealnoteService dealnoteService;
	

	private String cid;
	private String typeid;
	private List<Usergroup> uglist;
	private List<User> ulist;
	private DealtypeService dealtypeService;
	private List<Dealtype> dealtypeList;
	private String u;
	private String g;
	private Set<Dealnote> dealnoteSet;
	private Set<Task> taskSet;
	private TaskService taskService;
	private String state;
	
	
	public String execute(){

		dealList1 = dealService.findByProperty("state","跟踪");
		dealList2 = dealService.findByProperty("state","成功");
		dealList3 = dealService.findByProperty("state","失败");
		
		return "success";
	}
	public String edit(){
		deal = dealService.findById(id);
		contactsList = contactsService.findAll();
		uglist = usergroupService.findAllUsergroups();
		ulist = userService.findAll();
		dealtypeList = dealtypeService.findAll();
		return "success";
	}
	public String save(){
		Deal d = new Deal();
		Message message = new Message();
		if (deal.getView().equals("me")) {
			String view = "me"+","+uid;
			d.setView(view);
			message.setView(view);
		} else if (deal.getView().equals("use")){
			String view = "use"+","+u;
			d.setView(view);
			message.setView(view);
		}else if(deal.getView().equals("group")){
			String view = "group"+","+g;
			d.setView(view);
			message.setView(view);
		}else if(deal.getView().equals("all")){
			d.setView("all");
			message.setView("all");
		}
		d.setId(UUIDUtil.getUUID());
		d.setMoney(deal.getMoney());
		d.setCreatetime(DateUtil.getTime());
		d.setContent(deal.getContent());
		User user = userService.findAUser(uid);
		Contacts c = contactsService.findA(cid);
		d.setContacts(c);
		d.setUser(user);
		d.setTitle(deal.getTitle());
		d.setState("跟踪");

		Dealtype dt = dealtypeService.findADealtype(typeid);
		d.setDealtype(dt);
		dealService.save(d);

		
		
		message.setContent(deal.getContent());
		message.setCreatetime(TimeUtil.getTime());
		String title = deal.getTitle()+","+"deal.action";
		message.setTitle(title);
		message.setUser(user);
		
		String type = d.getState()+","+dt.getColor();
		message.setType(type);
		message.setCtdid(d.getId());
		message.setEnable(true);
		messageService.save(message);
		return "success";
	}
	public String del(){
		dealService.delete(id);
		Message message = messageService.findA(id);
		message.setEnable(false);
		messageService.update(message);
		return "success";
	}
	
	public String show(){
		
		deal = dealService.findById(id);
		dealnoteSet = deal.getDealnoteSet();
		taskSet = deal.getTaskSet();
		
		contacts = contactsService.findA(deal.getContacts().getId());
	
		System.out.println(dealnoteSet.size());
		return "success";
	}
	
	public String add(){
		contactsList = contactsService.findAll();
		uglist = usergroupService.findAllUsergroups();
		ulist = userService.findAll();
		dealtypeList = dealtypeService.findAll();
		
		return "success";
	}

	
	public String update(){
		Deal d = new Deal();
		HttpServletRequest request = ServletActionContext.getRequest();
		String cid = request.getParameter("cid");
		Message message = new Message();
		if (deal.getView().equals("me")) {
			String view = "me"+","+uid;
			d.setView(view);
			message.setView(view);
		} else if (deal.getView().equals("use")){
			String view = "use"+","+u;
			d.setView(view);
			message.setView(view);
		}else if(deal.getView().equals("group")){
			String view = "group"+","+g;
			d.setView(view);
			message.setView(view);
		}else if(deal.getView().equals("all")){
			d.setView("all");
			message.setView("all");
		}
		
		
		d.setId(deal.getId());
		d.setMoney(deal.getMoney());
		d.setCreatetime(DateUtil.getTime());
		d.setTitle(deal.getTitle());
		d.setContent(deal.getContent());
		d.setState(deal.getState());
		User user = userService.findAUser(uid);
		Contacts c = contactsService.findA(cid);
		
		d.setContacts(c);
		d.setUser(user);
		Dealtype dt = dealtypeService.findADealtype(typeid);
		d.setDealtype(dt);
		
		dealService.update(d);
		
		message.setContent(d.getContent());
		message.setCreatetime(TimeUtil.getTime());
		message.setTitle(d.getTitle());
		message.setUser(user);
		
		String type = d.getState()+","+dt.getColor();
		message.setType(type);
		message.setCtdid(d.getId());
		message.setEnable(true);
		messageService.update(message);
		
		
		
		
		
		return "success";
	}
	public String change(){
		
			Task task = taskService.findById(id);
			task.setComplete(true);
			taskService.save(task);
			return "success";
		
		
	}
	
	public String statechange(){
		Deal deal = dealService.findById(did);
		deal.setState(state);
		dealService.update(deal);
		return "success";
	}

	//set get
	
	public DealService getDealService() {
		return dealService;
	}
	public String getU() {
		return u;
	}
	public void setU(String u) {
		this.u = u;
	}
	public String getG() {
		return g;
	}
	public void setG(String g) {
		this.g = g;
	}
	public String getTypeid() {
		return typeid;
	}
	public void setTypeid(String typeid) {
		this.typeid = typeid;
	}
	public List<Usergroup> getUglist() {
		return uglist;
	}
	public void setUglist(List<Usergroup> uglist) {
		this.uglist = uglist;
	}
	public List<User> getUlist() {
		return ulist;
	}
	public void setUlist(List<User> ulist) {
		this.ulist = ulist;
	}
	@Autowired
	public void setDealtypeService(DealtypeService dealtypeService) {
		this.dealtypeService = dealtypeService;
	}
	@Autowired
	public void setDealService(DealService dealService) {
		this.dealService = dealService;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public Deal getDeal() {
		return deal;
	}
	public void setDeal(Deal deal) {
		this.deal = deal;
	}

	public String getCid() {
		return cid;
	}
	public void setCid(String cid) {
		this.cid = cid;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	@Autowired
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}
	@Autowired
	public void setUsergroupService(UsergroupService usergroupService) {
		this.usergroupService = usergroupService;
	}

	public List<Deal> getDealList1() {
		return dealList1;
	}
	public void setDealList1(List<Deal> dealList1) {
		this.dealList1 = dealList1;
	}
	public List<Deal> getDealList2() {
		return dealList2;
	}
	public void setDealList2(List<Deal> dealList2) {
		this.dealList2 = dealList2;
	}
	public List<Deal> getDealList3() {
		return dealList3;
	}
	public void setDealList3(List<Deal> dealList3) {
		this.dealList3 = dealList3;
	}
	public ContactsService getContactsService() {
		return contactsService;
	}
	@Autowired
	public void setContactsService(ContactsService contactsService) {
		this.contactsService = contactsService;
	}
	public List<Contacts> getContactsList() {
		return contactsList;
	}
	public void setContactsList(List<Contacts> contactsList) {
		this.contactsList = contactsList;
	}
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	

	public Contacts getContacts() {
		return contacts;
	}
	public void setContacts(Contacts contacts) {
		this.contacts = contacts;
	}
	public String getDid() {
		return did;
	}
	public void setDid(String did) {
		this.did = did;
	}
	public List<Dealnote> getDealnoteList() {
		return dealnoteList;
	}
	public void setDealnoteList(List<Dealnote> dealnoteList) {
		this.dealnoteList = dealnoteList;
	}
	public DealnoteService getDealnoteService() {
		return dealnoteService;
	}
	@Autowired
	public void setDealnoteService(DealnoteService dealnoteService) {
		this.dealnoteService = dealnoteService;
	}
	public Set<Dealnote> getDealnoteSet() {
		return dealnoteSet;
	}
	public void setDealnoteSet(Set<Dealnote> dealnoteSet) {
		this.dealnoteSet = dealnoteSet;
	}
	public Set<Task> getTaskSet() {
		return taskSet;
	}
	public void setTaskSet(Set<Task> taskSet) {
		this.taskSet = taskSet;
	}
	public List<Dealtype> getDealtypeList() {
		return dealtypeList;
	}
	public void setDealtypeList(List<Dealtype> dealtypeList) {
		this.dealtypeList = dealtypeList;
	}
	@Autowired
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	
	
}
